﻿using System;
using System.Collections.Generic;
using System.Windows.Controls;
using System.Linq;
using System.Windows.Data;
using System.Windows;

using System.Windows.Documents;
using System.Collections.ObjectModel;

namespace QuanLyDuocPham.Controllers.QuanLyHeThong.NhapXuat
{
    class DDHXUAT_Control
    {
        
        DuocPhamDataContext db = new DuocPhamDataContext(staticClass.Conn);
        public void loadComboBox(ComboBox cmb)
        {

            cmb.Items.Clear();
            var list = db.PHIEUXUAT_Get_List();
            var typeorder = list.OrderBy(w => w.NGAY);

            foreach (var t in typeorder)
            {
                cmb.Items.Add(t);
            }
            cmb.SelectedValuePath = "MA_PHIEUXUAT";
            cmb.IsTextSearchEnabled = true;
            cmb.IsTextSearchCaseSensitive = false;
            cmb.IsEditable = true;
            
        }

        public string loadID_moi()
        {
            string ID = "";

            int rowcount = db.PHIEUXUAT_Get_List().Count();
            if (rowcount == 0)
            {
                ID = "MX00000001";
            }
            else
            {
                for (int i = 1; i < rowcount+2; i++)
                {
                    string IDtam = "MX";
                    for (int j = 0; j < (8 - i.ToString().Length); j++)
                        IDtam = IDtam + "0";
                    IDtam = IDtam + i;
                    Boolean kq = true;
                    var data = db.PHIEUXUAT_Get_List();

                    foreach (var t in data)
                    {

                        if (t.MA_PHIEUXUAT.Trim() == IDtam.Trim())

                            kq = false;
                    }
                    if (kq == true)
                    {
                        ID = IDtam;
                        break;
                    }
                }

            }
            return ID;

        }


        public void addData(string txtMa_DDHXuat, ComboBox cmbKho, ComboBox cmbKhachHang, ComboBox cmbNhanVien, ComboBox cmbNhaThuoc, ComboBox cmbQuay, DatePicker dtpNgay, DatePicker dtpNgay_Giao, ComboBox cmbTinhTrang, ComboBox cmbLoai_HD, CheckBox chbCoHD, CheckBox chbDaXoa, DatePicker dtpHanTToan, TextBox txtDaTToan)
        {
            if (validateData() == false)
                MessageBox.Show("Chưa nhập đủ thông tin!");
            else
            {
                var cmbKhoSelected = (KHO_Get_ListResult)cmbKho.SelectedItem;
                var cmbKhachHangSelected = (KHACHHANG_Get_ListResult)cmbKhachHang.SelectedItem;
                var cmbNhanVienSelected = (NHANVIEN_Get_ListResult)cmbNhanVien.SelectedItem;
                var cmbNhaThuocSelected = (NHATHUOC_Get_ListResult)cmbNhaThuoc.SelectedItem;
                var cmbQuaySelected = (QUAYTN_Get_ListResult)cmbQuay.SelectedItem;
                db.PHIEUXUAT_Insert(txtMa_DDHXuat, cmbKhoSelected.MA_KHO, cmbKhachHangSelected.MA_KHACHHANG, cmbNhanVienSelected.MA_NHANVIEN, cmbNhaThuocSelected.MA_NHATHUOC, cmbQuaySelected.MA_QUAY, dtpNgay.SelectedDate, dtpNgay_Giao.SelectedDate, cmbTinhTrang.SelectedValue.ToString(), cmbLoai_HD.SelectedValue.ToString(), chbCoHD.IsChecked, dtpHanTToan.SelectedDate, int.Parse(txtDaTToan.Text), false);
                MessageBox.Show("Đã thêm thành công");
            }
        }
        private bool validateData()
        {
            
            return true;
        }

        public void loadData(ListView List)
        {
            List.Items.Clear();

            var list = db.PHIEUXUAT_Get_List();
            var typeorder = list.OrderBy(w => w.NGAY);

            foreach (var t in typeorder)
            {
                if (t.DA_XOA == false)
                    List.Items.Add(t);
            }
            
        }
        public void loadData(DataPager dp)
        {
            

            var list = db.PHIEUXUAT_Get_List();
            var typeorder = list.OrderBy(w => w.NGAY);

            ObservableCollection<Object> dpSource = new ObservableCollection<Object>();
            int i = 1;
            foreach (var t in typeorder)
            {

                dpSource.Add(new { STT = i, t.CO_HD, t.DA_TTOAN2, t.DA_XOA, t.HAN_TTOAN, t.LOAI_HD, t.MA_KHACHHANG, t.MA_KHO, t.MA_NHANVIEN, t.MA_NHATHUOC, t.MA_PHIEUXUAT, t.MA_QUAY, t.NGAY, t.NGAY_GIAO, t.TINH_TRANG });
                    i++;
                
            }
            dp.ItemsSource = dpSource;
            dp.ItemsPerPage = 10;
        }
        public void updateData(string txtMa_DDHXuat, ComboBox cmbKho, ComboBox cmbKhachHang, ComboBox cmbNhanVien, ComboBox cmbNhaThuoc, ComboBox cmbQuay, DatePicker dtpNgay, DatePicker dtpNgay_Giao, ComboBox cmbTinhTrang, ComboBox cmbLoai_HD, CheckBox chbCoHD, CheckBox chbDaXoa, DatePicker dtpHanTToan, TextBox txtDaTToan)
        {
            if (validateData() == false)
                MessageBox.Show("Chưa nhập đủ thông tin!");
            else
            {
                var cmbKhoSelected = (KHO_Get_ListResult)cmbKho.SelectedItem;
                var cmbKhachHangSelected = (KHACHHANG_Get_ListResult)cmbKhachHang.SelectedItem;
                var cmbNhanVienSelected = (NHANVIEN_Get_ListResult)cmbNhanVien.SelectedItem;
                var cmbNhaThuocSelected = (NHATHUOC_Get_ListResult)cmbNhaThuoc.SelectedItem;
                var cmbQuaySelected = (QUAYTN_Get_ListResult)cmbQuay.SelectedItem;
                db.PHIEUXUAT_Update(txtMa_DDHXuat,txtMa_DDHXuat, cmbKhoSelected.MA_KHO, cmbKhachHangSelected.MA_KHACHHANG, cmbNhanVienSelected.MA_NHANVIEN, cmbNhaThuocSelected.MA_NHATHUOC, cmbQuaySelected.MA_QUAY, dtpNgay.SelectedDate, dtpNgay_Giao.SelectedDate, cmbTinhTrang.SelectedValue.ToString(), cmbLoai_HD.SelectedValue.ToString(), chbCoHD.IsChecked, dtpHanTToan.SelectedDate, int.Parse(txtDaTToan.Text), false);
                MessageBox.Show("Đã cập nhật!");
            }
        }

        public string deleteData(string ID_Delete, string error)
        {

            error = "";
            if (db.PHIEUXUAT_GetByMaPhieuxuat(ID_Delete).Count() > 0)
            {
                var sl = (PHIEUXUAT_GetByMaPhieuxuatResult)db.PHIEUXUAT_GetByMaPhieuxuat(ID_Delete).Single();
                db.PHIEUXUAT_Update(ID_Delete, ID_Delete, sl.MA_KHO, sl.MA_KHACHHANG, sl.MA_NHANVIEN, sl.MA_NHATHUOC, sl.MA_QUAY, sl.NGAY, sl.NGAY_GIAO, sl.TINH_TRANG, sl.LOAI_HD, sl.CO_HD, sl.HAN_TTOAN, int.Parse(sl.DA_TTOAN2.ToString()),true);
                error = "Xóa thành công!";
            }
            else
            {
                db.PHIEUXUAT_Delete(ID_Delete);
                error = "Xóa thành công!";
            }
            return error;

        }
        DanhMuc.KHO_Control ctrl1 = new DanhMuc.KHO_Control();
        DanhMuc.KHACHHANG_Control ctrl2 = new DanhMuc.KHACHHANG_Control();
        DanhMuc.NHANVIEN_Control ctrl3 = new DanhMuc.NHANVIEN_Control();
        DanhMuc.NHATHUOC_Control ctrl4 = new DanhMuc.NHATHUOC_Control();
        DanhMuc.QUAY_Control ctrl5 = new DanhMuc.QUAY_Control();
        public void loadForm_Edit(string ID_Edit,  ComboBox cmbKho, ComboBox cmbKhachHang, ComboBox cmbNhanVien, ComboBox cmbNhaThuoc, ComboBox cmbQuay, DatePicker dtpNgay, DatePicker dtpNgay_Giao, ComboBox cmbTinhTrang, ComboBox cmbLoai_HD, CheckBox chbCoHD, CheckBox chbDaXoa, DatePicker dtpHanTToan, TextBox txtDaTToan)
        {

            var sl = (PHIEUXUAT_GetByMaPhieuxuatResult)db.PHIEUXUAT_GetByMaPhieuxuat(ID_Edit).Single();


            

            ctrl1.loadComboBox(cmbKho);
            int t = 0;
            foreach (var i in cmbKho.Items)
            {
                var temp = (KHO_Get_ListResult)i;
                if (temp.MA_KHO == sl.MA_KHO)
                {
                    break;
                }
                t++;
            }
            cmbKho.SelectedIndex = t;

            t = 0;
            ctrl2.loadComboBox(cmbKhachHang);
            foreach (var i in cmbKhachHang.Items)
            {
                var temp = (KHACHHANG_Get_ListResult)i;
                if (temp.MA_KHACHHANG == sl.MA_KHACHHANG)
                {
                    break;
                }
                t++;
            }
            cmbKhachHang.SelectedIndex = t;

            t = 0;
            ctrl3.loadComboBox(cmbNhanVien);
            foreach (var i in cmbNhanVien.Items)
            {
                var temp = (NHANVIEN_Get_ListResult)i;
                if (temp.MA_NHANVIEN == sl.MA_NHANVIEN)
                {
                    break;
                }
                t++;
            }
            cmbNhanVien.SelectedIndex = t;

            t = 0;
            ctrl4.loadComboBox(cmbNhaThuoc);
            foreach (var i in cmbNhaThuoc.Items)
            {
                var temp = (NHANVIEN_Get_ListResult)i;
                if (temp.MA_NHATHUOC == sl.MA_NHATHUOC)
                {
                    break;
                }
                t++;
            }
            cmbNhaThuoc.SelectedIndex = t;

            t = 0;
            ctrl5.loadComboBox(cmbQuay);
            foreach (var i in cmbQuay.Items)
            {
                var temp = (QUAYTN_Get_ListResult)i;
                if (temp.MA_QUAY == sl.MA_QUAY)
                {
                    break;
                }
                t++;
            }
            cmbQuay.SelectedIndex = t;

            dtpNgay.SelectedDate = sl.NGAY;
            chbCoHD.IsChecked = sl.CO_HD;
            cmbTinhTrang.Items.Add(new { MA_TT = "DA GIAO", TEN_TT = "Đã giao" });
            cmbTinhTrang.Items.Add(new { MA_TT = "CHUA GIAO", TEN_TT = "Chưa giao" });
            cmbTinhTrang.Items.Add(new { MA_TT = "BI TRA LAI", TEN_TT = "Bị trả lại" });
            int u = 0;
            foreach (var i in cmbTinhTrang.Items)
            {
                var temp = (TableRow)i;
                if (temp.Cells[0].ToString().Trim() == sl.TINH_TRANG.Trim())
                {
                    break;
                }
                u++;
            }

            cmbTinhTrang.SelectedIndex = u;


            cmbLoai_HD.Items.Add(new { MA_LHD = "BAN LE", TEN_LHD = "Bán lẻ" });
            cmbLoai_HD.Items.Add(new { MA_LHD = "BAN SI", TEN_LHD = "Bán Sỉ" });
            int v = 0;
            foreach (var i in cmbLoai_HD.Items)
            {
                var temp = (TableRow)i;
                if (temp.Cells[0].ToString().Trim() == sl.LOAI_HD.Trim())
                {
                    break;
                }
                v++;
            }

            cmbLoai_HD.SelectedIndex = v;
            dtpNgay_Giao.SelectedDate = sl.NGAY_GIAO;
            chbDaXoa.IsChecked = sl.DA_XOA;
            dtpHanTToan.SelectedDate = sl.HAN_TTOAN;
            txtDaTToan.Text = sl.DA_TTOAN2.ToString();

        }
    }
}
